<?php

namespace app\admin\controller;
use think\Db;
use think\Config;
use think\Loader;
use think\Session;

class Index extends Base
{
    public function index()
    {
        $this->assign ('web_site_icp',config('web_site_icp'));
        $this->assign ('username',session('username'));
        return $this->fetch('/index');
    }


    public function indexPage()
    {
        if(session('myarea')){
            $place = Db::name('area')->where('district_id',session('myarea'))->find();
            $area = $place['district'];
            if($place['level'] == 1){//省
                $info = ['省代理','区代理','商户','用户'];
                $all = Db::name('area')->where('pid',session('myarea'))->column ('district_id');
                $all = Db::name('area')->where('pid','in',$all)->column ('district_id');
                $sheng = Db::name('admin')->where('id',session('uid'))->value('integral');
                $qu = Db::name('admin')->where('area','in',$all)->sum('integral');
                $shop = Db::name('shop')->where('shop_superior_agent','in',$all)->sum('shop_integral');
                $user = Db::name('u_user')->where('user_area','in',$all)->sum('user_integral');
                $midd = Db::name('d_jifen')->where(['co_from'=>['in',$all],'co_status'=>1])->sum("co_jifen");
                $arr = [
                    ['value'=>$sheng,'name'=>'省代理'],
                    ['value'=>$qu,'name'=>'区代理'],
                    ['value'=>$shop,'name'=>'商户'],
                    ['value'=>$user+$midd,'name'=>'用户']
                ];
                $type = "省代理";
            }else{//区
                $info = ['区代理','商户','用户'];
                $qu = Db::name('admin')->where('id',session('uid'))->value('integral');
                $midd = Db::name('d_jifen')->where(['co_from'=>session ('myarea'),'co_status'=>1])->sum("co_jifen");
                $shop = Db::name('shop')->where('shop_qu_id',session('myarea'))->sum('shop_integral');
                $user = Db::name('jifen')->where('jifen_from',session('myarea'))->sum('jifen_number');
                $arr = [
                    ['value'=>$qu,'name'=>'区代理'],
                    ['value'=>$shop,'name'=>'商户'],
                    ['value'=>$user+$midd,'name'=>'用户']
                ];
                $type = "区/县代理";
            }
        }else{//总部
            $area = '总部';
            $info = ['总部','省代理','区代理'];
            $integral = Db::name('admin')->where('id',1)->value('integral');
            $sheng = Db::name('admin')->where('type',2)->sum('integral');
            $qu = Db::name('admin')->where('type',3)->sum('integral');
            $arr = [
                ['value'=>$integral,'name'=>'总部'],
                ['value'=>$sheng,'name'=>'省代理'],
                ['value'=>$qu,'name'=>'区代理']
            ];
            $type = "";
        }
        $data = Db::name('admin')->where('id',session('uid'))->find();
        $this->assign ('data',$data);
        $this->assign ('area',$area);
        $this->assign ('type',$type);
        $this->assign ('info',json_encode ($info));
        $this->assign ('arr',json_encode ($arr));
        return $this->fetch('index');
    }

    public function clear() {
        if (delete_dir_file(CACHE_PATH) && delete_dir_file(TEMP_PATH)) {
            return json(['code' => 200, 'msg' => '清除缓存成功']);
        } else {
            return json(['code' => 100, 'msg' => '清除缓存失败']);
        }
    }


}
